#!/bin/sh

# small separator
s()
{
	echo " ^c#FF0000^^r0,0,1,19^^f5^^d^"
}

# big separator
S()
{
	echo " ^c#FFFF00^^r0,0,2,19^^f10^^d^"
}

cpu()
{
	CPU_US=$(top -bn1 | awk 'NR==3{print $2}')
	CPU_SY=$(top -bn1 | awk 'NR==3{print $4}')
	CPU_USAGE=$(echo $CPU_US + $CPU_SY | bc | xargs printf "%.*f" 0)
	if [ $CPU_USAGE -le 1 ]; then
		echo "^c#226622^CPU: ^r0,0,7,19^^f5^^d^^r-5,17,7,0^^f5^"
	elif [ $CPU_USAGE -le 10 -a $CPU_USAGE -gt 1 ]; then
		echo "^c#04FF00^CPU:^d^ ^c#226622^^r0,0,7,19^^f5^^d^^c#04FF00^^r-5,17,7,2^^f5^^d^"
	elif [ $CPU_USAGE -le 20 -a $CPU_USAGE -gt 10 ]; then
		echo "^c#0BFF00^CPU:^d^ ^c#226622^^r0,0,7,19^^f5^^d^^c#0BFF00^^r-5,15,7,4^^f5^^d^"
	elif [ $CPU_USAGE -le 30 -a $CPU_USAGE -gt 20 ]; then
		echo "^c#BBBB00^CPU:^d^ ^c#666622^^r0,0,7,19^^f5^^d^^c#BBBB00^^r-5,13,7,6^^f5^^d^"
	elif [ $CPU_USAGE -le 40 -a $CPU_USAGE -gt 30 ]; then
		echo "^c#BBBB00^CPU:^d^ ^c#666622^^r0,0,7,19^^f5^^d^^c#BBBB00^^r-5,11,7,8^^f5^^d^"
	elif [ $CPU_USAGE -le 50 -a $CPU_USAGE -gt 40 ]; then
		echo "^c#FFFF00^CPU:^d^ ^c#886622^^r0,0,7,19^^f5^^d^^c#FFFF00^^r-5,9,7,10^^f5^^d^"
	elif [ $CPU_USAGE -le 60 -a $CPU_USAGE -gt 50 ]; then
		echo "^c#FFFF00^CPU:^d^ ^c#886622^^r0,0,7,19^^f5^^d^^c#FFFF00^^r-5,7,7,12^^f5^^d^"
	elif [ $CPU_USAGE -le 70 -a $CPU_USAGE -gt 60 ]; then
		echo "^c#FFBB00^CPU:^d^ ^c#AA6622^^r0,0,7,19^^f5^^d^^c#FFBB00^^r-5,5,7,14^^f5^^d^"
	elif [ $CPU_USAGE -le 80 -a $CPU_USAGE -gt 70 ]; then
		echo "^c#FFBB00^CPU:^d^ ^c#AA2222^^r0,0,7,19^^f5^^d^^c#FFBB00^^r-5,3,7,16^^f5^^d^"
	elif [ $CPU_USAGE -le 90 -a $CPU_USAGE -gt 80 ]; then
		echo "^c#FF0000^CPU:^d^ ^c#BB2222^^r0,0,7,19^^f5^^d^^c#FF0000^^r-5,1,7,18^^f5^^d^"
	elif [ $CPU_USAGE -gt 90 ]; then
		echo "^c#FF0000^CPU:^d^ ^c#BB2200^^r0,0,7,19^^f5^^d^^c#FF0000^^r-5,0,7,20^^f5^^d^"
	fi
}

memory()
{
	# ram memory
	RAM_USED=$(free -h | awk 'NR==2{print $3 " / " $7}')
	RAM=$(free -g | awk 'NR==2{print $3}')
	if [ $RAM -lt 1 ]; then
		RAM_USED="^c#00FF00^ $RAM_USED^d^"
	elif [ $RAM -ge 1 -a $RAM -lt 2 ]; then
		RAM_USED="^c#80FF00^ $RAM_USED^d^"
	elif [ $RAM -ge 2 -a $RAM -lt 3 ]; then
		RAM_USED="^c#BFFF00^ $RAM_USED^d^"
	elif [ $RAM -ge 3 -a $RAM -lt 4 ]; then
		RAM_USED="^c#FFFF00^ $RAM_USED^d^"
	elif [ $RAM -ge 4 -a $RAM -lt 5 ]; then
		RAM_USED="^c#FFBF00^ $RAM_USED^d^"
	elif [ $RAM -ge 5 -a $RAM -lt 6 ]; then
		RAM_USED="^c#FF8000^ $RAM_USED^d^"
	elif [ $RAM -ge 6 -a $RAM -lt 7 ]; then
		RAM_USED="^c#FF4000^ $RAM_USED^d^"
	elif [ $RAM -ge 7 ]; then
		RAM_USED="^c#FF0000^ $RAM_USED^d^"
	fi

	# swap
	SWAP=$(free | awk 'NR==3{print $3}')
	SWAP_USED=$(free -h | awk 'NR==3{print "/ " $3}')
	if [ $SWAP -gt 0 ]; then
		SWAP_USED="^c#FF0000^$SWAP_USED^d^"
	else 
		SWAP_USED="^c#00FF00^$SWAP_USED^d^"
	fi

	# disk
	DISK=$(df -h | awk 'NR==4{print $3 " / " $4}')
	echo "$RAM_USED $SWAP_USED $(s) ^c#FFFFFF^ $DISK^d^"
}

wifi()
{
	LINK=$(cat /proc/net/wireless | awk 'NR==3{print substr($3,1,2)}')
	WIFI=$(cat /proc/net/wireless | awk 'NR==3{print substr($3,1,2)}')
	WIFI=$(echo "$WIFI * 10 / 7" | bc)
	if [ $WIFI -ge 90 -a $LINK -gt 60 ]; then
		WIFI="^c#00FF00^直 $WIFI^d^"
	elif [ $WIFI -lt 90 -a $WIFI -ge 80 ]; then
		WIFI="^c#40FF00^直 $WIFI^d^"
	elif [ $WIFI -lt 80 -a $WIFI -ge 70 ]; then
		WIFI="^c#80FF00^直 $WIFI^d^"
	elif [ $WIFI -lt 70 -a $WIFI -ge 60 ]; then
		WIFI="^c#BFFF00^直 $WIFI^d^"
	elif [ $WIFI -lt 60 -a $WIFI -ge 50 ]; then
		WIFI="^c#FFFF00^直 $WIFI^d^"
	elif [ $WIFI -lt 50 -a $WIFI -ge 40 ]; then
		WIFI="^c#FFFF00^直 $WIFI^d^"
	elif [ $WIFI -lt 40 -a $WIFI -ge 30 ]; then
		WIFI="^c#FFBF00^直 $WIFI^d^"
	elif [ $WIFI -lt 30 -a $WIFI -ge 20 ]; then
		WIFI="^c#FF8000^直 $WIFI^d^"
	elif [ $WIFI -lt 20 -a $WIFI -ge 10 ]; then
		WIFI="^c#FF4000^直 $WIFI^d^"
	elif [ $WIFI -lt 10 -a $LINK -lt 10]; then
		WIFI="^c#FF4000^直 $WIFI^d^"
	else
		WIFI="^c#FF0000^睊 x_x^d^"
	fi
	echo "$WIFI"
}

basic() 
{
	HOUR=$(date "+%H:%M")
	#HOUR="^c#FFFFFF^ $HOUR^d^"
	HOUR="^c#FFFFFF^$HOUR^d^"

	# volume
	VOL=$(amixer -c 1 get Master | awk '/Mono./ {print substr($4,2,2)}') 
	VOL_STATE=$(amixer -c 1 get Master | awk '/Mono./ {print substr($6,3,1)}') 
	if [ "$VOL_STATE" = "n" ]; then
		VOL="^c#FFFFFF^墳 $VOL^d^"
	else
		VOL="^c#FF0000^婢 $VOL^d^"
	fi

	# bright
	BRI=$(xbacklight | xargs printf "%.*f" 0)
	BRI="^c#FFFFFF^$BRI^d^"

	# battery
	BAT=$(cat /sys/class/power_supply/BAT1/capacity)
	STATUS=$(cat /sys/class/power_supply/BAT1/status)
	if [ $STATUS != "Discharging" ]; then
		BAT="^c#00FF00^ $BAT^d^"
	else
		if [ $BAT -ge 90 ]; then
			BAT="^c#00FF00^ $BAT^d^"
		elif [ $BAT -lt 90 -a $BAT -ge 80 ]; then
			BAT="^c#40FF00^ $BAT^d^"
		elif [ $BAT -lt 80 -a $BAT -ge 70 ]; then
			BAT="^c#40FF00^ $BAT^d^"
		elif [ $BAT -lt 70 -a $BAT -ge 60 ]; then
			BAT="^c#80FF00^ $BAT^d^"
		elif [ $BAT -lt 60 -a $BAT -ge 50 ]; then
			BAT="^c#BFFF00^ $BAT^d^"
		elif [ $BAT -lt 50 -a $BAT -ge 40 ]; then
			BAT="^c#FFFF00^ $BAT^d^"
		elif [ $BAT -lt 40 -a $BAT -ge 30 ]; then
			BAT="^c#FFBF00^ $BAT^d^"
		elif [ $BAT -lt 30 -a $BAT -ge 20 ]; then
			BAT="^c#FF8000^ $BAT^d^"
		elif [ $BAT -lt 20 -a $BAT -ge 10 ]; then
			BAT="^c#FF4000^ $BAT^d^"
		elif [ $BAT -lt 10 ]; then
			BAT="^c#FF0000^ $BAT^d^"
		fi
	fi

	# print informations
	echo "$VOL $(s) ^c#FBF547^ﯦ^d^ $BRI $(s) $BAT $(s) $HOUR" 
}

while true
do
	xsetroot -name "$(cpu) $(S) $(memory) $(S) $(wifi) $(S) $(basic)"
	sleep 1
done
